package com.sg.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sg.vo.IntegralRankingVO;
import com.sg.common.ResponseInfo;
import com.sg.entity.*;
import com.sg.service.*;
import com.sg.vo.ProgramVO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/info")
public class PointInfoController {
    @Resource
    private PointInfoService pointInfoService;
    @Resource
    private NewsService newsService;
    @Resource
    private ProgramService programService;
    @Resource
    private IntegralFlowService integralFlowService;
    @Resource
    private LoginService loginService;
    @Resource
    private UserTagService userTagService;
    @Resource
    private DzwlService dzwlService;


    @GetMapping("/point/getInfo")
    public ResponseInfo<PointInfo> getInfo(Integer id){
        PointInfo byId = pointInfoService.getById(id);
        return ResponseInfo.success(byId);
    }

    @GetMapping("/point/detail")
    public ResponseInfo<String> infoDetai(Integer id){
        PointInfo byId = pointInfoService.getById(id);
        return ResponseInfo.success(byId.getIntroduction());
    }

    @GetMapping("/news/list")
    public ResponseInfo<List<News>> newsList(){
        LambdaQueryWrapper<News> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(News::getSaveTime);
        List<News> list = newsService.list(wrapper);
        return ResponseInfo.success(list);
    }

    @GetMapping("/news/detail")
    public ResponseInfo<String> detail(Integer id){
        News byId = newsService.getById(id);
        return ResponseInfo.success(byId.getContent());
    }

    @GetMapping("/program/list")
    public ResponseInfo<List<ProgramVO>> programList(){
        LambdaQueryWrapper<Program> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Program::getStartTime);
        List<Program> list = programService.list(wrapper);
        List<ProgramVO> list1 = new ArrayList<>();
        list.forEach(program -> {
            ProgramVO programVO = new ProgramVO();
            BeanUtils.copyProperties(program, programVO);
            programVO.setDate(program.getStartTime());
            programVO.setTime(DateUtil.format(program.getStartTime(), "HH:mm")+"—"+DateUtil.format(program.getStopTime(), "HH:mm") );
            list1.add(programVO);
        });
        return ResponseInfo.success(list1);
    }


    @GetMapping("/program/detail")
    public ResponseInfo<String> programDetail(Integer id){
        Program byId = programService.getById(id);
        return ResponseInfo.success(byId.getContent());
    }

    @GetMapping("/integral/myIntegral")
    public ResponseInfo<IntegralRankingVO> myIntegral(HttpServletRequest request) {
        UserInfo loginYh = loginService.getLoginYh(request);
        LambdaQueryWrapper<IntegralFlow> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IntegralFlow::getUserId, loginYh.getId());
        List<IntegralFlow> list = integralFlowService.list(wrapper);
        // 积分数量
        int sum = list.stream().mapToInt(IntegralFlow::getChangUnm).sum();
        // 排名
        String rank = integralFlowService.getPm(loginYh.getId());

        // 已经完成几个互动
        wrapper.clear();
        wrapper.eq(IntegralFlow::getUserId, loginYh.getId()).isNotNull(IntegralFlow::getArriveInfo);
        List<IntegralFlow> list1 = integralFlowService.list(wrapper);
        Set<String> collect = list1.stream().map(IntegralFlow::getArriveInfo).collect(Collectors.toSet());

        IntegralRankingVO build = IntegralRankingVO.builder().integral(sum).ranking(rank==null?"暂无":rank).finished(collect.size()).build();
        return ResponseInfo.success(build);
    }

    @GetMapping("/dw/wddw")
    public ResponseInfo<String> wddw(HttpServletRequest request){
        UserInfo loginYh = loginService.getLoginYh(request);
        // 查询当前用户绑定的手环
        LambdaQueryWrapper<UserTag> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserTag::getUserId, loginYh.getId()).eq(UserTag::getStatus, 1).orderByDesc(UserTag::getBondTime).last("limit 1");
        UserTag one = userTagService.getOne(wrapper);
        if(one == null){
            return ResponseInfo.success("-1");
        }
        LambdaQueryWrapper<Dzwl> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Dzwl::getTagId, one.getTagId()).orderByDesc(Dzwl::getSj).last("limit 1");
        Dzwl one1 = dzwlService.getOne(wrapper1);
        if(one1 == null){
            return ResponseInfo.success("-1");
        }
        return ResponseInfo.success(one1.getInfo());

    }


    @PostMapping("saveDzwl")
    public ResponseInfo<Boolean> saveDzwl(@RequestBody Dzwl dzwl){
        dzwlService.save(dzwl);
        integralFlowService.saveFlow(dzwl);
        return ResponseInfo.success(true);
    }

}
